package com.bcpapp.project.controller;

import com.bcpapp.project.common.BaseResponse;
import com.bcpapp.project.common.ErrorCode;
import com.bcpapp.project.common.ResultUtils;
import com.bcpapp.project.exception.BusinessException;
import com.bcpapp.project.model.dto.post.PostBuyTicketRequest;
import com.bcpapp.project.model.dto.post.PostWicketRequest;
import com.bcpapp.project.service.impl.TicketServiceImpl;
import com.bcpapp.project.utils.SM4Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;


@Slf4j
@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Value("${key}")
    private String key;

    @Resource
    TicketServiceImpl ticketService;

    @PostMapping("/testjiemi")
    public String testjiemi(String miwen) throws NoSuchPaddingException, IllegalBlockSizeException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, NoSuchProviderException {
        System.out.println("miwne+++++++++++" + miwen);
        String mingwne = SM4Util.decrypt(key, miwen);
        System.out.println("解密后+++++++++++" + mingwne);
        return mingwne;
    }


    @PostMapping("/testjiami")
    public String testjiami(String mingwne) throws NoSuchPaddingException, IllegalBlockSizeException, IOException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, NoSuchProviderException {
        System.out.println("miwne+++++++++++" + mingwne);
        String miwen = SM4Util.encrypt(key, mingwne);
        System.out.println("解密后+++++++++++" + miwen);
        return miwen;
    }

    /*
     *
     * 购票接口
     * */
    @PostMapping("/postBuyTicket")
    public BaseResponse<Boolean> postBuyTicket(@Validated @RequestBody PostBuyTicketRequest postBuyTicketRequest) {


        try {
            ticketService.postBuyTicketService(postBuyTicketRequest);
        } catch (Exception e) {
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "密钥解密失败");
            e.printStackTrace();
        }
        return ResultUtils.success(true);
    }

    /*
     *
     * 检票接口
     * */
    @PostMapping("/postCheckTicket")
    public BaseResponse<Boolean> postWicket(@Validated @RequestBody PostWicketRequest postWicketRequest) {


        try {
            ticketService.postCheckTicketService(postWicketRequest);
        } catch (Exception e) {
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "密钥解密失败");
            e.printStackTrace();
        }
        return ResultUtils.success(true);
    }
}
